package model

import "time"

type Data struct {
	ID          int64   `json:"id" gorm:"primaryKey;autoIncrement"`
	Temperature float64 `json:"temperature" gorm:"column:temperature"`
	Wet         float64 `json:"wet" gorm:"column:wet"`
	CreateTime  int64   `json:"create_time" gorm:"column:create_time"`
}

func (*Data) TableName() string {
	return "data"
}

func DataTableName() string {
	return "data"
}

func (d *Data) Insert() error {
	d.CreateTime = time.Now().UnixMilli()
	err := defaultDB.Table(d.TableName()).Create(d).Error
	return err
}

func GetDataByIDRange(id int64, limit int64) ([]*Data, error) {
	result := []*Data{}
	err := defaultDB.Table(DataTableName()).Where("id > ?", id).Find(&result).Limit(int(limit)).Error
	return result, err
}

func DeleteByID(id int64) error {
	return defaultDB.Table(DataTableName()).Where("id = ?", id).Delete(&Data{}).Error
}

func RunSql(sql string) error {
	return defaultDB.Exec(sql).Error
}
